package com.xhsj.user.sms.purchase.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhsj.user.sms.base.BaseService;
import com.xhsj.user.sms.purchase.entity.TbPurchaseProject;
import com.xhsj.user.sms.purchase.entity.TbPurchaseUserProject;
import com.xhsj.user.sms.purchase.mapper.TbPurchaseProjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 采购项目 服务实现类
 * </p>
 *
 * @author suxiaolin
 * @since 2020-06-11
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbPurchaseProject")
public class TbPurchaseProjectService extends BaseService<TbPurchaseProjectMapper, TbPurchaseProject>{
    @Autowired
    private TbPurchaseProjectMapper dao;

    @Autowired
    private TbPurchaseUserProjectService tbPurchaseUserProjectService;

    @Override
    //@CacheEvict(cacheNames="TbPurchaseProject", allEntries=true)
    public boolean save(TbPurchaseProject entity) {
        return super.save(entity);
    }

    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbPurchaseProject getById(Serializable id) {
        return super.getById(id);
    }


    public boolean removeById(String id) throws Exception {
        //删除项目表数据
        if(dao.deleteProjectById(id)==0){
            throw new Exception("保存采购申请清单表失败");
        }
        //删除用户与项目表的数据
        tbPurchaseUserProjectService.deleteUserProjectByProcessId(id);
        return true;
    }

    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    //@Cacheable(key = "#data",sync = true)
    public IPage<TbPurchaseProject> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbPurchaseProject entity = JSON.parseObject(JSON.toJSONString(data), TbPurchaseProject.class);
        Page<TbPurchaseProject> page = new Page(currentPage,pageSize);
        QueryWrapper<TbPurchaseProject> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

    @Override
    public List<TbPurchaseProject> selectAll() {
        return super.selectAll();
    }


//    采购项目,保存/修改
    public boolean saveProject(TbPurchaseProject data) throws Exception {
        //删除用户与项目表的数据
        tbPurchaseUserProjectService.deleteUserProjectByProcessId(data.getId());
        data.setUpdateBy(data.getCreateBy());
       if(super.save(data)){
           String projectId = data.getId();
           List<List<String>> projectUserIds =  data.getProjectUserIds();
           for (List<String> userId:projectUserIds
                ) {
               TbPurchaseUserProject tbPurchaseUserProject=new TbPurchaseUserProject();
               tbPurchaseUserProject.setCreateBy(data.getCreateBy());
               tbPurchaseUserProject.setUpdateBy(data.getCreateBy());
               tbPurchaseUserProject.setDeptId(String.valueOf(userId.get(0)));
               tbPurchaseUserProject.setUserId(String.valueOf(userId.get(1)));
               tbPurchaseUserProject.setProjectId(projectId);
               if(!tbPurchaseUserProjectService.save(tbPurchaseUserProject)){
                   throw new Exception("保存采购申请清单表失败");
               }
           }
       }
       return true;
    }
}
